package com.project.imageinterference.controller;

import com.project.imageinterference.exception.BusinessException;
import com.project.imageinterference.service.ImageService;
import com.project.imageinterference.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Description 图片处理控制层
 * @Author cwhao
 * @Date 2025/2/6 下午2:03
 */
@Api(tags = "图片处理接口")
@RestController
public class ImageController {

    @Autowired
    private ImageService imageService;

    /**
     * 上传图片
     * @param file web上传的图片文件
     * @return Result
     * @author cwhao
     * @date 2025/2/6 下午2:31
     **/
//    @PostMapping("/upload2")
//    public Result<List<ImageIdentifyVO>> uploadImage(@RequestParam("file") MultipartFile file) {
//        try {
//            // 调用服务层处理图片
//            List<ImageIdentifyVO> result = imageService.processImage(file);
//            return Result.success(result);
//        }catch (BusinessException e) {
//            return Result.failure(e.getMessage());
//        } catch (Exception e) {
//            return Result.failure("处理图片失败: " + e.getMessage());
//        }
//    }

    /**
     * 上传并解析图片
     * @param file web上传的图片文件
     * @return
     * @author cwhao
     * @date 2025/2/15 下午2:31
     */
    @ApiOperation(value = "上传并解析图片")
    @PostMapping("/upload")
    public ResponseEntity<String> uploadImageToRemoteServer(@RequestParam("file") MultipartFile file) {
        try {
            ResponseEntity<String> responseEntity = imageService.uploadImageToRemoteServer(file);
            return responseEntity;
        }catch (BusinessException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("上传并解析图片失败: ");
        }
    }

    @ApiOperation(value = "测试连通性接口")
    @GetMapping("/testApi")
    public Result<String> testApi(){
        return Result.success("接口测试");
    }

    /**
     * 上传图片转换成base64
     * @param file web上传的图片文件
     * @return
     * @author cwhao
     * @date 2025/2/22 下午17:32
     */
    @ApiOperation(value = "上传图片转换成base64")
    @PostMapping("/uploadImageToConvertBase64")
    public Result uploadImageToConvertBase64(@RequestParam("file") MultipartFile file) {
        try {
            // 调用服务层处理图片
            String result = imageService.uploadImageToConvertBase64(file);
            return Result.success(result);
        }catch (Exception e) {
            return Result.failure("图片转换base64失败: " + e.getMessage());
        }
    }
}
